<template>
  <div>
    <div v-if="OutOrderKeys != '666'" id="components-form-demo-advanced-search">
      <a-row :gutter="24" style="margin-bottom: 15px">
        <a-col :span="6">
          客户编号：<a-select
            show-search
            option-filter-prop="children"
            style="width: 60%"
            :filter-option="filterOption"
            v-model="searchData.tenantId"
          >
            <a-select-option
              :value="item.tenantId"
              v-for="item in accArray"
              :key="item.id"
              :titles="item.title"
            >
              {{ item.companyName + "/" + item.tenantId }}
            </a-select-option>
          </a-select>
        </a-col>
        <a-col :span="6">
          订单号：<a-input
          :maxLength="128"
            style="width: 60%"
            v-model="searchData.orderNumber"
          />
        </a-col>
        <a-col :span="6">
          运单号：<a-input :maxLength="128" style="width: 60%" v-model="searchData.waybillNo" />
        </a-col>
        <a-col :span="6">
          <div v-show="!expand">
            <a-button
              type="primary"
              class="searchBtn"
              @click="
                () => {
                  getOutOrderList();
                }
              "
            >
              查询
            </a-button>
            <a-button
              style="margin: 0 10px"
              type="primary"
              class="searchBtn"
              @click="resetSearch"
            >
              重置
            </a-button>
            <a
              :style="{ marginRight: '8px', fontSize: '14px' }"
              @click="expand = !expand"
            >
              {{ expand ? "折叠" : "展开" }}
              <a-icon :type="expand ? 'up' : 'down'" />
            </a>
          </div>
          <div v-show="expand">
            入库仓库：<a-select
              show-search
              option-filter-prop="children"
              style="width: 60%"
              :filter-option="filterOption"
              v-model="searchData.warehouseId"
              allowClear
            >
              <a-select-option
                :value="item.warehouseId"
                v-for="item in warehouse"
                :key="item.warehouseId"
              >
                {{ item.titleName }}
              </a-select-option>
            </a-select>
          </div>
        </a-col>
      </a-row>
      <a-row :gutter="24" v-show="expand" style="margin-bottom: 15px">
        <a-col :span="6">
          物流商：<a-select
            show-search
            option-filter-prop="children"
            style="width: 60%"
            :filter-option="filterOption"
            v-model="searchData.logisticsId"
          >
            <a-select-option
              v-for="(item, i) in logisticsArr"
              :value="
                item.sysLogisticsId + ',' + item.sysLogisticsServiceItemId
              "
              :key="i"
              :logisticsPlatform="item.logisticsPlatform"
              :serviceName="item.serviceName"
              :sysLogisticsId="item.sysLogisticsId"
              :sysLogisticsServiceItemId="item.sysLogisticsServiceItemId"
            >
              {{ item.serviceName }}/{{ item.logisticsPlatform }}
            </a-select-option>
          </a-select>
        </a-col>
        <a-col :span="6">
          自有运单号：<a-input :maxLength="128" style="width: 53%" v-model="searchData.ectnNo" />
        </a-col>
        <a-col :span="6">
          更新时间：<a-range-picker
            v-model="orderDate"
            @change="onStartDateChange"
            style="width: 60%"
          />
        </a-col>
        <a-col :span="6"> 提交时间：<a-range-picker
            v-model="submitDate"
            @change="onsubmitDateChange"
            style="width: 60%"
          /> </a-col>
      </a-row>
      <a-row :gutter="24" v-show="expand">
        <a-col :span="6">
          发货时间：<a-range-picker
            v-model="deliveryTime"
            @change="ondDeliveryTimeChange"
            style="width: 60%"
          />
        </a-col>
        <a-col :span="6">
          <div v-show="expand">
            <a-button
              type="primary"
              class="searchBtn"
              @click="
                () => {
                  getOutOrderList();
                }
              "
            >
              查询
            </a-button>
            <a-button
              style="margin: 0 10px"
              type="primary"
              class="searchBtn"
              @click="resetSearch"
            >
              重置
            </a-button>
            <a
              :style="{ marginRight: '8px', fontSize: '14px' }"
              @click="expand = !expand"
            >
              {{ expand ? "折叠" : "展开" }}
              <a-icon :type="expand ? 'up' : 'down'" />
            </a>
          </div>
        </a-col>
        <a-col :span="6"> </a-col>

        <a-col :span="6"> </a-col>
      </a-row>
    </div>
    <a-spin tip="Loading..." :spinning="spinning">
      <a-tabs default-active-key="15" @change="callback">
        <a-tab-pane key="15">
          <span slot="tab">
            <a-badge
              :count="statecount === null ? 0 : statecount.disposeCount"
              :offset="[15, 0]"
              >待处理
            </a-badge>
          </span>
          <p class="filtrationData"></p>
          <OutOrderTable
            OutOrderKeys="待处理"
            v-if="OutOrderKeys === '15'"
            :tableData="tableData"
            :getStateCount="getStateCount"
            :getOutOrderList="getOutOrderList"
            @selectedRowKeys="selectedRowKeys"
          />
        </a-tab-pane>
        <a-tab-pane key="25" force-render>
          <span slot="tab">
            <a-badge
              :count="statecount === null ? 0 : statecount.processedCount"
              :offset="[15, 0]"
              >处理中
            </a-badge>
          </span>
          <p class="filtrationData">
            <span class="filtrationBtn">
              <a-button
                @click="filtrateLsit('0')"
                :style="
                  searchDataFlag === '0'
                    ? 'color: #61ba8f;border-color: #61ba8f;border: 1px solid #61ba8f'
                    : 'color: #626262;border-color: #fff;border: 1px solid #ccc'
                "
              >
                所有
              </a-button>
              <a-button
                @click="filtrateLsit('1')"
                :style="
                  searchDataFlag === '1'
                    ? 'color: #61ba8f;border-color: #61ba8f;border: 1px solid #61ba8f'
                    : 'color: #626262;border-color: #fff;border: 1px solid #ccc'
                "
              >
                复合订单
              </a-button>
              <a-button
                @click="filtrateLsit('2')"
                :style="
                  searchDataFlag === '2'
                    ? 'color: #61ba8f;border-color: #61ba8f;border: 1px solid #61ba8f'
                    : 'color: #626262;border-color: #fff;border: 1px solid #ccc'
                "
              >
                预生成运单
              </a-button>
              <a-button
                @click="filtrateLsit('3')"
                :style="
                  searchDataFlag === '3'
                    ? 'color: #61ba8f;border-color: #61ba8f;border: 1px solid #61ba8f'
                    : 'color: #626262;border-color: #fff;border: 1px solid #ccc'
                "
              >
                未生成运单
              </a-button>
              <a-button
                @click="filtrateLsit('4')"
                :style="
                  searchDataFlag === '4'
                    ? 'color: #61ba8f;border-color: #61ba8f;border: 1px solid #61ba8f'
                    : 'color: #626262;border-color: #fff;border: 1px solid #ccc'
                "
              >
                已生成运单
              </a-button>
              <a-button
                @click="filtrateLsit('5')"
                :style="
                  searchDataFlag === '5'
                    ? 'color: #61ba8f;border-color: #61ba8f;border: 1px solid #61ba8f'
                    : 'color: #626262;border-color: #fff;border: 1px solid #ccc'
                "
              >
                非系统运单
              </a-button>
              <a-button
                @click="filtrateLsit('6')"
                :style="
                  searchDataFlag === '6'
                    ? 'color: #61ba8f;border-color: #61ba8f;border: 1px solid #61ba8f'
                    : 'color: #626262;border-color: #fff;border: 1px solid #ccc'
                "
              >
                未打印运单
              </a-button>
              <a-button
                @click="filtrateLsit('7')"
                :style="
                  searchDataFlag === '7'
                    ? 'color: #61ba8f;border-color: #61ba8f;border: 1px solid #61ba8f'
                    : 'color: #626262;border-color: #fff;border: 1px solid #ccc'
                "
              >
                未列入批次
              </a-button>
              <a-button
                @click="filtrateLsit('8')"
                :style="
                  searchDataFlag === '8'
                    ? 'color: #61ba8f;border-color: #61ba8f;border: 1px solid #61ba8f'
                    : 'color: #626262;border-color: #fff;border: 1px solid #ccc'
                "
              >
                批次处理单
              </a-button>
            </span>
          </p>
          <OutOrderTable
            OutOrderKeys="处理中"
            :tableData="tableData"
            v-if="OutOrderKeys === '25'"
            :getStateCount="getStateCount"
            :getOutOrderList="getOutOrderList"
            @selectedRowKeys="selectedRowKeys"
          />
        </a-tab-pane>
        <a-tab-pane key="45">
          <span slot="tab">
            <a-badge
              :count="statecount === null ? 0 : statecount.exceptionCount"
              :offset="[15, 0]"
              >异常订单
            </a-badge>
          </span>
          <OutOrderTable
            OutOrderKeys="异常订单"
            v-if="OutOrderKeys === '45'"
            :tableData="tableData"
            :getStateCount="getStateCount"
            :getOutOrderList="getOutOrderList"
            @selectedRowKeys="selectedRowKeys"
          />
        </a-tab-pane>
        <a-tab-pane key="55">
          <span slot="tab">
            <a-badge
              :count="statecount === null ? 0 : statecount.processingCount"
              :offset="[15, 0]"
              >暂缓处理
            </a-badge>
          </span>

          <OutOrderTable
            OutOrderKeys="暂缓处理"
            v-if="OutOrderKeys === '55'"
            :tableData="tableData"
            :getStateCount="getStateCount"
            :getOutOrderList="getOutOrderList"
            @selectedRowKeys="selectedRowKeys"
          />
        </a-tab-pane>
        <a-tab-pane key="35">
          <span slot="tab">
            <a-badge
              :count="statecount === null ? 0 : statecount.shippedCount"
              :offset="[15, 0]"
              >已发货
            </a-badge>
          </span>

          <OutOrderTable
            OutOrderKeys="已发货"
            v-if="OutOrderKeys === '35'"
            :tableData="tableData"
            :getStateCount="getStateCount"
            :getOutOrderList="getOutOrderList"
            @selectedRowKeys="selectedRowKeys"
          />
        </a-tab-pane>
        <a-tab-pane key="666" style="padding: 10px">
          <span slot="tab"> 批次管理 </span>
          <BatchManage
            v-if="OutOrderKeys === '666'"
            :warehouse="warehouse"
            :getOutBatchList="getOutBatchList"
          />
        </a-tab-pane>
        <!-- 待仓库处理 -->
        <span
          class="action-btn"
          v-if="OutOrderKeys === '15'"
          slot="tabBarExtraContent"
        >
          <a-dropdown :trigger="['click']">
            <a-menu slot="overlay" @click="handleLogisticsClick">
              <a-menu-item key="1"> 所有物流订单 </a-menu-item>
              <a-menu-item :key="item" v-for="item in logisticsProvider">
                所有{{ item }}订单
              </a-menu-item>
            </a-menu>
            <a-button
              style="
                background: #66b1ff;
                color: #fff;
                border: none;
                margin-right: 20px;
                margin-left: 8px;
              "
              type="primary"
            >
              开始处理 <a-icon type="down" />
            </a-button>
          </a-dropdown>
        </span>
        <!-- 正在处理 -->
        <span
          class="action-btn"
          v-if="OutOrderKeys === '25'"
          slot="tabBarExtraContent"
        >
          请选择批次分配：
          <a-select
            show-search
            placeholder="选择进行批次分配"
            option-filter-prop="children"
            style="width: 160px"
            :filter-option="filterOption"
            v-model="bachNo"
          >
            <a-select-option
              @click="handleChangeBatchList(item.bachNo, item.bachName)"
              :value="item.bachNo"
              v-for="item in batchList"
              :key="item.id"
              :bachName="item.bachName"
            >
              <a-tooltip>
                <template slot="title">
                  {{ item.warehouseName }} | {{ item.bachName }}
                </template>
                {{ item.warehouseName }} | {{ item.bachName }}
              </a-tooltip>
            </a-select-option>
          </a-select>
          <a-dropdown :trigger="['click']">
            <a-menu slot="overlay" @click="handleShipmentsClick">
              <a-menu-item key="1"> 所有物流订单 </a-menu-item>
              <a-menu-item :key="item" v-for="item in logisticsProvider">
                所有{{ item }}订单
              </a-menu-item>
            </a-menu>
            <a-button
              style="
                background: #66b1ff;
                color: #fff;
                border: none;
                margin-left: 8px;
              "
              type="primary"
            >
              确认发货 <a-icon type="down" />
            </a-button>
          </a-dropdown>
          <a-dropdown :trigger="['click']">
            <a-menu slot="overlay" @click="handleMenuClick">
              <a-menu-item key="defer"> 暂缓处理 </a-menu-item>
              <a-menu-item key="abnormal"> 异常处理 </a-menu-item>
              <a-menu-item key="release"> 释放批次 </a-menu-item>
              <a-menu-item key="create"> 生成运单 </a-menu-item>
              <a-menu-item key="batchWaybill"> 下载运单 </a-menu-item>
              <a-menu-item key="print"> 打印拣货单 </a-menu-item>
              <!-- <a-menu-item key="editOrder"> 编辑运单信息 </a-menu-item> -->
            </a-menu>
            <a-button
              style="
                background: #66b1ff;
                color: #fff;
                border: none;
                margin-right: 20px;
                margin-left: 8px;
              "
              type="primary"
            >
              批量操作 <a-icon type="down" />
            </a-button>
          </a-dropdown>
        </span>
        <!-- 异常订单 -->
        <span
          class="action-btn"
          v-if="OutOrderKeys === '45'"
          slot="tabBarExtraContent"
        >
          <a-dropdown :trigger="['click']">
            <a-menu slot="overlay" @click="handleMenuClick">
              <a-menu-item key="start"> 开始处理 </a-menu-item>
              <a-menu-item key="defer"> 暂缓处理 </a-menu-item>
            </a-menu>
            <a-button
              style="
                background: #66b1ff;
                color: #fff;
                border: none;
                margin-right: 20px;
              "
              type="primary"
            >
              批量操作 <a-icon type="down" />
            </a-button>
          </a-dropdown>
        </span>
        <!-- 暂缓处理 -->
        <span
          class="action-btn"
          v-if="OutOrderKeys === '55'"
          slot="tabBarExtraContent"
        >
          <a-dropdown :trigger="['click']">
            <a-menu slot="overlay" @click="handleMenuClick">
              <a-menu-item key="start"> 开始处理 </a-menu-item>
              <a-menu-item key="abnormal"> 异常处理 </a-menu-item>
            </a-menu>
            <a-button
              style="
                background: #66b1ff;
                color: #fff;
                border: none;
                margin-left: 8px;
                margin-right: 20px;
              "
              type="primary"
            >
              批量操作 <a-icon type="down" />
            </a-button>
          </a-dropdown>
        </span>
        <!-- 已发货 -->
        <span
          class="action-btn"
          v-if="OutOrderKeys === '35'"
          slot="tabBarExtraContent"
        >
          <a-button
            style="
              background: #66b1ff;
              color: #fff;
              border: none;
              margin-left: 8px;
            "
            @click="printPDF"
            type="primary"
          >
            打印PDF运单
          </a-button>
          <a-button
            type="primary"
            style="
              background: #66b1ff;
              color: #fff;
              border: none;
              margin-left: 8px;
              margin-right: 20px;
            "
            @click="handleMenuClick('print')"
          >
            导出订单
          </a-button>
        </span>
      </a-tabs>
    </a-spin>
    <!-- 分页器 -->
    <div
      style="margin: 20px 0; text-align: center"
      v-if="OutOrderKeys != '666'"
    >
      <a-config-provider :locale="locale">
        <a-pagination
          :current="searchData.current"
          show-size-changer
          :pageSizeOptions="['10', '20', '30']"
          :show-total="
            (total) => `${$t('m.page.Total')} ${total} ${$t('m.page.items')}`
          "
          @showSizeChange="showChange"
          :buildOptionText="
            (props) => ` ${props.value} ${$t('m.page.a')}/${$t('m.page.page')}`
          "
          show-quick-jumper
          :default-current="searchData.current"
          :total="total"
          @change="onChangePage"
        />
      </a-config-provider>
    </div>
    <!-- //弹出框 -->
    <OutOrderModal
      :getStateCount="getStateCount"
      :getOutOrderList="getOutOrderList"
      :OutOrderKeys="OutOrderKeys"
    />
    <!-- 批量导入模态框 -->
    <!-- <BatchImportModal :getOutOrderList="getOutOrderList"/> -->
  </div>
</template>

<script>
import { mapState } from "vuex";
import req from "@/utils/req.js";
import { selectAllLogistics } from "@/apis/common";
import BatchManage from "@/components/system/OutOrder/BatchManage.vue";
import moment from "moment";
import OutOrderModal from "@/components/system/OutOrder/OutOrderModal.vue";
// import BatchImportModal from "@/components/system/OutOrder/BatchImportModal.vue";
import {
  OutOrder_statecount,
  OutBound_page,
  OutBound_batchList,
  OutBound_updateBatch,
  OutBound_updateDefer,
  OutBound_releaseBatch,
  OutBound_excelout,
  OutBound_createWaybill,
  OutBound_delivery,
  OutBound_confirm,
  OutBound_dowWay,
} from "@/apis/system/WM/OutOrder";
import OutOrderTable from "@/components/system/OutOrder/OutOrderTable.vue";
import locale from "ant-design-vue/es/date-picker/locale/zh_CN";
export default {
  components: {
    OutOrderTable,
    OutOrderModal,
    BatchManage,
    // BatchImportModal,
  },
  data() {
    return {
      orderDate: null,
      searchDataFlag: "0",
      spinning: false,
      expand: false,
      //批次号
      bachNo: "",
      //表格复选框值
      selectedKeys: [],
      selectedRows: [],
      //批次：
      batchList: [],
      // 气泡数量
      statecount: { submitCount: 1 },
      //物流商
      logisticsArr: [],
      // 表格key
      OutOrderKeys: "15",
      locale,
      total: 5,
      tableData: [],
      searchData: {
        applyState: "15",
        batchNo: null,
        batchState: null,
        isEctn: null,
        waybillPrint: null,
        current: 1,
        ectnNo: null,
        endtDate: null,
        id: null,
        logisticsCost: null,
        logisticsId: null,
        orderCode: null,
        orderNumber: null,
        recipient: null,
        size: 10,
        startDate: null,
        tenantId: null,
        warehouseId: null,
        waybillNo: null,
        waybillState: null,
        submitEndDate:null,
        submitStartDate:null,
        deliveryStartTime:null,
        deliveryEndTime:null,
      },
      submitDate:null,
      deliveryTime:null,
      BatchValue: {
        value: null,
        option: null,
      },
      //物流商
      logisticsProvider: [],
    };
  },
  created() {
    this.getOutBatchList();
    this.getOutOrderList();
    this.getLogistics();
  },
  mounted() {},
  methods: {
    
    filtrateLsit(flag) {
      this.searchDataFlag = flag;
      for (let key in this.searchData) {
        this.searchData[key] = null;
      }
      this.searchData.applyState = this.OutOrderKeys;
      this.searchData.current = 1;
      this.searchData.size = 10;
      if (flag === "0") {
        this.getOutOrderList();
      } else if (flag === "1") {
        this.searchData.orderCode = "1";
        this.getOutOrderList();
      } else if (flag === "2") {
        this.searchData.waybillState = "3";
        this.getOutOrderList();
      } else if (flag === "3") {
        this.searchData.waybillState = "0";
        this.getOutOrderList();
      } else if (flag === "4") {
        this.searchData.waybillState = "1";
        this.getOutOrderList();
      } else if (flag === "5") {
        this.searchData.isEctn = "1";
        this.getOutOrderList();
      } else if (flag === "6") {
        this.searchData.waybillPrint = "0";
        this.getOutOrderList();
      } else if (flag === "7") {
        this.searchData.batchState = "0";
        this.getOutOrderList();
      } else if (flag === "8") {
        this.searchData.batchState = "1";
        this.getOutOrderList();
      }
    },
    //打印pDf运单
    async printPDF() {
      if (this.selectedKeys.length == 0) {
        this.$message.error("请勾选需要批量操作得数据！");
        return false;
      }
      this.name = "";
      this.selectedKeys.forEach((item) => {
        this.name += `pam=${item}&`;
      });
      let res = await OutBound_dowWay(this.name);
      req.exportPDF("运单文件", res);
    },
    // 接收子传过来的复选框值
    selectedRowKeys(selectedKeys, selectedRows) {
      this.selectedKeys = selectedKeys;
      this.selectedRows = selectedRows;
    },
    //批次分配
    handleChangeBatchList(value, option) {
      this.BatchValue.value = value;
      this.BatchValue.option = option;
      let _this = this;
      if (this.selectedKeys.length === 0) {
        this.bachNo = null;
        this.$message.error("请选择要分配批次的数据");
        return false;
      }

      this.$confirm({
        title: `确定将选中的这些数据加入${option}批次吗?`,
        cancelText: "取消",
        okText: "确定",
        onOk() {
          return new Promise((resolve) => {
            setTimeout(async () => {
              let { code } = await OutBound_updateBatch(
                `/by-outbound-order/update-bathno/${value}`,
                _this.selectedKeys
              );
              if (code === "10000") {
                resolve();
              } else {
                resolve();
              }
            }, 1000);
          }).then(() => {
            setTimeout(() => {
              _this.$bus.$emit("OutOrderSelectedKeys");
            }, 50);
            _this.getOutOrderList();
            _this.bachNo = "";
            _this.selectedKeys = [];
          });
        },
      });
    },
    // //重置按钮
    resetSearch() {
      let current = this.searchData.current;
      let size = this.searchData.size;
      for (let key in this.searchData) {
        this.searchData[key] = null;
      }
      this.searchData.applyState = this.OutOrderKeys;
      this.searchData.current = current;
      this.searchData.size = size;
      this.orderDate = null;
      this.submitDate = null;
      this.deliveryTime = null;
      this.getOutOrderList();
    },
    disabledDate(current) {
      // Can not select days before today and today
      return (
        current && current < moment(this.searchData.startDate).endOf("day")
      );
    },
    //   选项卡
    callback(key) {
      if (key === "25") {
        this.getOutBatchList();
      }
      this.selectedKeys = [];
      if (key === "25") {
        this.searchDataFlag = "0";
      }
      for (let key in this.searchData) {
        this.searchData[key] = null;
      }
      this.searchData.current = 1;
      this.searchData.size = 10;
      this.OutOrderKeys = key;
      this.searchData.applyState = key;
      this.getOutOrderList();
    },
    //分页器事件
    onChangePage(pageNumber) {
      this.searchData.current = pageNumber;
      this.getOutOrderList();
    },
    //页码变更
    showChange(size, current) {
      this.searchData.current = size;
      this.searchData.size = current;
      this.getOutOrderList();
    },
    //批量操作
    handleMenuClick(e) {
      let _this = this;

      if (this.selectedKeys.length == 0) {
        this.$message.error("请勾选需要批量操作得数据！");
        return false;
      }
      if (e.key === "defer") {
        //批量暂缓处理
        this.$confirm({
          title: "确定将这些数据设置为暂缓处理吗?",
          okText: "确定",
          cancelText: "取消",
          onOk() {
            return new Promise((resolve) => {
              setTimeout(async () => {
                let { code } = await OutBound_updateDefer(
                  "55",
                  _this.selectedKeys
                );
                if (code === "10000") {
                  resolve();
                }
              }, 1000);
            }).then(() => {
              _this.getOutOrderList();
            });
          },
        });
      } else if (e.key === "abnormal") {
        //批量异常处理
        this.$confirm({
          title: "确定将这些数据设置为异常处理吗?",
          okText: "确定",
          cancelText: "取消",
          onOk() {
            return new Promise((resolve) => {
              setTimeout(async () => {
                let { code } = await OutBound_updateDefer(
                  "45",
                  _this.selectedKeys
                );
                if (code === "10000") {
                  resolve();
                }
              }, 1000);
            }).then(() => {
              _this.getOutOrderList();
            });
          },
        });
      } else if (e.key === "release") {
        _this.passFlag = true;
        _this.selectedRows.forEach((item) => {
          if (item.batchNo === null) {
            _this.passFlag = false;
          }
        });
        if (_this.passFlag) {
          // 批量释放批次
          this.$confirm({
            title: "确定将这些数据释放批次吗?",
            okText: "确定",
            cancelText: "取消",
            onOk() {
              return new Promise((resolve) => {
                setTimeout(async () => {
                  let { code } = await OutBound_releaseBatch(
                    _this.selectedKeys
                  );
                  if (code === "10000") {
                    resolve();
                  } else {
                    resolve();
                  }
                }, 1000);
              }).then(() => {
                _this.getOutOrderList();
              });
            },
          });
        } else {
          this.$message.error("选择的数据中有未分配批次的订单！");
          return false;
        }
      } else if (e.key === "create") {
        //批量生成运单
        this.$confirm({
          title: "确定要将这些数据生成运单吗?",
          okText: "确定",
          cancelText: "取消",
          onOk() {
            return new Promise((resolve) => {
              setTimeout(async () => {
                let { code, message } = await OutBound_createWaybill(
                  _this.selectedKeys
                );
                if (code === "10000") {
                  resolve();
                  _this.getOutOrderList();
                } else {
                  _this.$message.error(message);
                  resolve();
                }
              }, 1000);
            }).then(() => {});
          },
        });
      } else if (e.key === "print" || e === "print") {
        //批量打印拣货单
        this.$confirm({
          title: "确定要下载这些数据为Excle表吗?",
          okText: "下载",
          cancelText: "取消",
          onOk() {
            return new Promise((resolve) => {
              setTimeout(async () => {
                let res = await OutBound_excelout(_this.selectedKeys);
                req.exportFile("订单列表", res);
                resolve();
              }, 1000);
            }).then(() => {});
          },
        });
      }
      //  if (e.key === "editOrder") {
      // 编辑运单信息
      // setTimeout(() => {
      //   this.$bus.$emit("OutOrderBatch", this.selectedKeys);
      // }, 50);
      // } else
      else if (e.key === "start") {
        // 开始处理
        this.$confirm({
          title: "确定要处理这些订单吗?",
          okText: "确定",
          cancelText: "取消",
          onOk() {
            return new Promise((resolve) => {
              setTimeout(async () => {
                let { code } = await OutBound_updateDefer(
                  "25",
                  _this.selectedKeys
                );
                if (code === "10000") {
                  resolve();
                }
              }, 1000);
            }).then(() => {
              _this.getOutOrderList();
            });
          },
        });
      } else if (e.key === "batchWaybill") {
        _this.batchWaybillFlag = true;
        _this.selectedRows.forEach((item) => {
          if (item.waybillNo === null) {
            _this.batchWaybillFlag = false;
          }
        });
        if (_this.batchWaybillFlag) {
          this.$confirm({
            title: "确定要下载这些订单的运单吗?",
            okText: "确定",
            cancelText: "取消",
            onOk() {
              return new Promise((resolve) => {
                setTimeout(async () => {
                  _this.name = "";
                  _this.selectedKeys.forEach((item) => {
                    _this.name += `pam=${item}&`;
                  });
                  let res = await OutBound_dowWay(_this.name);
                  req.exportPDF("运单文件", res);
                  resolve();
                  // }
                }, 1000);
              }).then(() => {
                // _this.getOutOrderList();
                // _this.getStateCount();
              });
            },
          });
        } else {
          this.$message.error("选择的数据中有未生成运单/或是自有运单的订单！");
          return false;
        }
      }
    },
    //批量处理物流
    handleLogisticsClick(e) {
      let _this = this;
      if (e.key === "1") {
        let arrKey = [];
        this.tableData.forEach((item) => {
          arrKey.push(item.key);
        });
        this.$confirm({
          title: "确定处理所有物流订单吗?",
          okText: "确定",
          cancelText: "取消",
          onOk() {
            return new Promise((resolve, reject) => {
              setTimeout(async () => {
                let { code, message } = await OutBound_delivery(arrKey);
                if (code === "10000") {
                  resolve();
                } else {
                  reject();
                  this.$message.error(message);
                }
              }, 1000);
            }).then(() => {
              _this.getOutOrderList();
            });
          },
        });
      } else {
        let tableArr = this.tableData.filter((item) => {
          return item.logisticsName === e.key;
        });
        let arrKey = [];
        tableArr.forEach((item) => {
          arrKey.push(item.key);
        });
        this.$confirm({
          title: `确定处理${e.key}订单吗?`,
          okText: "确定",
          cancelText: "取消",
          onOk() {
            return new Promise((resolve, reject) => {
              setTimeout(async () => {
                let { code, message } = await OutBound_delivery(arrKey);
                if (code === "10000") {
                  resolve();
                } else {
                  reject();
                  this.$message.error(message);
                }
              }, 1000);
            }).then(() => {
              _this.getOutOrderList();
            });
          },
        });
      }
    },
    //批量处理物流
    handleShipmentsClick(e) {
      let _this = this;
      if (e.key === "1") {
        let arrKey = [];
        this.tableData.forEach((item) => {
          arrKey.push(item.key);
        });
        this.$confirm({
          title: "确定发货所有物流订单吗?",
          okText: "确定",
          cancelText: "取消",
          onOk() {
            return new Promise((resolve) => {
              setTimeout(async () => {
                let { code } = await OutBound_confirm(arrKey);
                if (code === "10000") {
                  resolve();
                } else {
                  resolve();
                }
              }, 1000);
            }).then(() => {
              _this.getOutOrderList();
            });
          },
        });
      } else {
        let tableArr = this.tableData.filter((item) => {
          return item.logisticsName === e.key;
        });
        let arrKey = [];
        tableArr.forEach((item) => {
          arrKey.push(item.key);
        });
        this.$confirm({
          title: `确定发货${e.key}订单吗?`,
          okText: "确定",
          cancelText: "取消",
          onOk() {
            return new Promise((resolve) => {
              setTimeout(async () => {
                let { code } = await OutBound_confirm(arrKey);
                if (code === "10000") {
                  resolve();
                }else{
                  resolve();
                }
              }, 1000);
            }).then(() => {
              _this.getOutOrderList();
            });
          },
        });
      }
    },
    //时间查询
    onStartDateChange(date, dateString) {
      this.searchData.startDate = dateString[0];
      this.searchData.endtDate = dateString[1];
      date;
    },
    //// 提交时间改变
    onsubmitDateChange(date, dateString) {
      this.searchData.submitStartDate = dateString[0];
      this.searchData.submitEndDate = dateString[1];
      date;
    },
  // 发货时间改变
    ondDeliveryTimeChange(date, dateString) {
      this.searchData.deliveryStartTime = dateString[0];
      this.searchData.deliveryEndTime = dateString[1];
      date;
    },
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text
          .toLowerCase()
          .indexOf(input.toLowerCase()) >= 0
      );
    },
    //获取表头统计
    async getStateCount() {
      let res = await OutOrder_statecount();
      if (res.code === "200") {
        this.statecount = res.responseBody;
      }
    },
    //获取表格数据
    async getOutOrderList() {
      this.logisticsProvider = [];
      if (this.OutOrderKeys === "666") {
        this.searchData.applyState = null;
      }
      this.spinning = true;
      let { code, responseBody } = await OutBound_page(this.searchData);
      if (code === "200") {
        this.total = responseBody.total * 1;
        let { records } = responseBody;
        this.tableData = JSON.stringify(records);
        this.tableData = JSON.parse(this.tableData);
        this.tableData.forEach((item) => {
          if (item.logisticsName != null) {
            this.logisticsProvider.push(item.logisticsName);
          }
          item.updateTime = item.updateTime
            ? item.updateTime.replace("T", "  ")
            : "";
          item.createTime = item.createTime
            ? item.createTime.replace("T", "  ")
            : "";
        });
        this.logisticsProvider = this.filtrationNewArr(this.logisticsProvider);
        this.getStateCount();
        this.spinning = false;
      }
    },
    //获取物流商
    async getLogistics() {
      let { code, responseBody } = await selectAllLogistics();
      if (code === "200") {
        this.logisticsArr = responseBody;
      }
    },
    async getOutBatchList() {
      let { code, responseBody } = await OutBound_batchList();
      if (code === "200") {
        this.batchList = responseBody;
      }
    },
    //数组去重
    filtrationNewArr(arr) {
      for (var i = 0; i < arr.length; i++) {
        for (var j = i + 1; j < arr.length; j++) {
          if (arr[i] == arr[j]) {
            //如果第一个等于第二个，splice方法删除第二个
            arr.splice(j, 1);
            j--;
          }
        }
      }
      return arr;
    },
  },
  computed: {
    ...mapState(["accArray"]),
    ...mapState(["warehouse"]),
  },
};
</script>

<style lang="less" scoped>
.searchBtn {
  background: #fff;
  color: #3eae7c;
  text-shadow: none;
}
.searchBtn:hover {
  background: #3eae7c;
  color: #fff;
}
.filtrationData {
  display: flex;
  justify-content: space-between;
  button {
    margin: 5px;
  }
}
</style>